<?php
/**
 * MyGravatar
 *
 * @author Davy hutushen222@gmail.com
 */
class MyGravatar
{

	/**
	 * MyGravatar
	 *
	 * @see http://gravatar.com/site/implement/images/php/
	 *
	 * @param string $email The gravatar email address
	 * @param integer $size Size in pixels, defaults to 80px [ 1 - 512 ]
	 * @param string $default Default image url | Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
	 * @param string $rating Maximum rating (inclusive) [ g | pg | r | x ]
	 * @param boolean $image True to return a complete IMG tag False for just the URL
	 * @param array $attributes Optional, additional key/value attributes to include in the IMG tag
	 */
	public function __construct($email, $size = 80, $default = 'mm', $rating = 'G', $image = false, $attributes = array())
	{
		$this->setEmail($email);
		$this->setSize($size);
		$this->setDefault($default);
		$this->setRating($rating);
		$this->setImage($image);
		$this->setAttributes($attributes);
	}

	public function setEmail($email)
	{
		$this->email = $this->sanitizeEmail($email);
	}

	public function setSize($size)
	{
		$size = (int)$size;
		$size > 0 ? $this->size = $size : $this->size = 80;
	}

	public function setDefault($default)
	{
		$this->default = $default;
	}

	public function setRating($rating)
	{
		$ratings = array("G", "PG", "R", "X");
		in_array(strtoupper($rating), $ratings) ? $this->rating = $rating : 'G';
	}

	public function setImage($image)
	{
		$this->image = (bool)$image;
	}

	public function setAttributes($attributes)
	{
		$this->attributes = $attributes;
	}

	public function __get($var)
	{
		return isset($this->$var) ? $this->$var : null;
	}

	public function __toString()
	{
		$url = 'http://www.gravatar.com/avatar/';
		$url .= md5(strtolower(trim($this->email)));
		$url .= "?s={$this->size}&d={$this->default}&r={$this->rating}";
		if ($this->image) {
			$url = '<img src="' . $url . '"';
			foreach ($this->attributes as $key => $value) {
				$url .= ' ' . $key . '="' . $value . '"';
			}
			$url .= ' />';
		}
		return $url;
	}

	private function sanitizeEmail($email)
	{
		// Source: http://www.zend.com/zend/spotlight/ev12apr.php
		// return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", trim($email)) ? trim($email) : '';
		return trim($email);
	}

	private $email = '';
	private $size = 80;
	private $default = 'mm';
	private $rating = 'G';
	private $image = false;
	private $attributes = array();

	/**
	 * Gravatar's URL
	 */
	const GRAVATAR_URL = 'http://www.gravatar.com/avatar/';
}

/**
 * Get either a Gravatar URL or complete image tag for a specified email address.
 *
 * @param string $email The email address
 * @param string $s Size in pixels, defaults to 80px [ 1 - 512 ]
 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
 * @param boole $img True to return a complete IMG tag False for just the URL
 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
 * @return String containing either just a URL or a complete image tag
 * @source http://gravatar.com/site/implement/images/php/
 */
function get_gravatar($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
{
	$url = 'http://www.gravatar.com/avatar/';
	$url .= md5(strtolower(trim($email)));
	$url .= "?s=$s&d=$d&r=$r";
	if ($img) {
		$url = '<img src="' . $url . '"';
		foreach ($atts as $key => $val)
			$url .= ' ' . $key . '="' . $val . '"';
		$url .= ' />';
	}
	return $url;
}
